package fragment.detialfragment;


import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.ScrollView;
import android.widget.TextView;

import com.google.gson.Gson;
import com.squareup.picasso.Picasso;

import java.io.IOException;
import java.util.List;

import bean.SpeakerItemBean;
import bean.SpeakerListViewBean;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import qf.wlh.com.oneprisdent.AllSpeakerContentActivity;
import qf.wlh.com.oneprisdent.PlayVideoActivity;
import qf.wlh.com.oneprisdent.R;
import qf.wlh.com.oneprisdent.adapter.SpeakerListViewAdapter;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;
import utils.RetrofitSpeakerListViewInterface;

/**
 * A simple {@link Fragment} subclass.
 */
public class SpeakerDetialFragment extends Fragment {
    private Retrofit retrofit;
    private ImageView imageBackGround;
    private ImageView imagetouxiang;
    private TextView textName;
    private TextView textType;
    private TextView textTitle;
    private TextView textTime;
    private TextView textPalce;
    private TextView textDesc;
    private ImageView imagePlay;
    private TextView textPlay;
    private TextView textLike;
    private TextView textPurecontent;
    private TextView TextAllData;
    private ListView listView;
    private SpeakerListViewAdapter adapter;
    private Handler handler=new Handler();
    private Button button;
    private  SpeakerItemBean.DataBean dataBean=new SpeakerItemBean.DataBean();
    private ImageView startImage;
    private String name;
    private ScrollView scrollView;


    public SpeakerDetialFragment() {
        // Required empty public constructor
    }

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment

        View view=inflater.inflate(R.layout.fragment_speaker_detial,container,false);

        initView(view);
        Bundle bundle = getArguments();
        int id = bundle.getInt("key");
        /////////////////////////////////////
        //下载解析listView的数据
        retrofit=new Retrofit.Builder()
                .addConverterFactory(GsonConverterFactory.create())
                .baseUrl("http://api.yixi.tv/api/v1/lecture/"+id+"/")
                .build();
        RetrofitSpeakerListViewInterface retrofitSpeakerListViewInterface =
                retrofit.create(RetrofitSpeakerListViewInterface.class);
        retrofitSpeakerListViewInterface.getData().enqueue(new Callback<SpeakerListViewBean>() {
            @Override
            public void onResponse(Call<SpeakerListViewBean> call, Response<SpeakerListViewBean> response) {
                List<SpeakerListViewBean.DataBean> data = response.body().getData();
                if (data!=null){
                    adapter=new SpeakerListViewAdapter(data,getContext());

                    int count=data.size();
                    ViewGroup.LayoutParams layoutParams = listView.getLayoutParams();
                    layoutParams.height=count*adapter.dip2px(100);
                    listView.setLayoutParams(layoutParams);
                    listView.setAdapter(adapter);
                    adapter.notifyDataSetChanged();

                }

            }

            @Override
            public void onFailure(Call<SpeakerListViewBean> call, Throwable t) {

            }
        });

        /////////用okhttp请求数据
        OkHttpClient okHttpClient=new OkHttpClient();
        Request request=new Request.Builder()
                .url("http://api.yixi.tv/api/v1/lecture/detail/"+id)
                .build();
        okhttp3.Call call = okHttpClient.newCall(request);
        call.enqueue(new okhttp3.Callback() {
            @Override
            public void onFailure(okhttp3.Call call, IOException e) {

            }

            @Override
            public void onResponse(okhttp3.Call call, okhttp3.Response response) throws IOException {
                Gson gson=new Gson();
                String str=new String(response.body().bytes());

                SpeakerItemBean bean = gson.fromJson(str, SpeakerItemBean.class);

                dataBean = bean.getData();
                if (response!=null){
                    if (dataBean!=null){
                        handler.post(new Runnable() {
                            @Override
                            public void run() {
                                initData(dataBean);
                            }
                        });

                  }
                }
            }
        });


        return view;
    }

    public void initData(SpeakerItemBean.DataBean dataBean){
        textName.setText(dataBean.getLecturer().getNickname());
        Picasso.with(getContext()).load(dataBean.getLecturer().getBackground()).into(imageBackGround);
        Picasso.with(getContext()).load(dataBean.getLecturer().getPic()).into(imagetouxiang);
        textType.setText(dataBean.getLecturer().getDesc());
        textTitle.setText(dataBean.getTitle());
        textTime.setText(dataBean.getTime());
        textPalce.setText(dataBean.getSite());
        textDesc.setText(dataBean.getDesc());
        Picasso.with(getContext()).load(dataBean.getCover()).into(imagePlay);
        textPalce.setText("播放"+dataBean.getViewnum());
        textLike.setText(dataBean.getLikenum()+"点赞");
        textPurecontent.setText(dataBean.getPurecontent());
        TextAllData.setText("共"+dataBean.getCmtnum());
        name=dataBean.getVideo();
    }
    private void initView(View view) {
        imageBackGround = ((ImageView) view.findViewById(R.id.speaker_background));
        imagetouxiang = ((ImageView) view.findViewById(R.id.speaker_touxiang));
        textName = (TextView) view.findViewById(R.id.speaker_name);
        textType = ((TextView) view.findViewById(R.id.speaker_type));
        textTitle = ((TextView) view.findViewById(R.id.text_title));
        textTime = ((TextView) view.findViewById(R.id.text_time));
        textPalce = ((TextView) view.findViewById(R.id.text_place));
        textDesc = ((TextView) view.findViewById(R.id.desc));
        imagePlay = ((ImageView) view.findViewById(R.id.image_vedio));
        textPlay = ((TextView) view.findViewById(R.id.speaker_play_time));
        textLike = ((TextView) view.findViewById(R.id.speaker_likenum));
        textPurecontent = ((TextView) view.findViewById(R.id.purecontent));
        TextAllData = ((TextView) view.findViewById(R.id.all_data));
        startImage = ((ImageView) view.findViewById(R.id.start_vedio));
        listView = ((ListView) view.findViewById(R.id.may_like_listview));
        button = ((Button) view.findViewById(R.id.load_more));
        scrollView = ((ScrollView) view.findViewById(R.id.scrollview));

        initlistener();

    }

    private void initlistener() {
        //设置scrollview不在顶端的问题
        scrollView.smoothScrollTo(0,0);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent=new Intent(getActivity(), AllSpeakerContentActivity.class);

                intent.putExtra("title",dataBean.getTitle());
                intent.putExtra("name",dataBean.getLecturer().getNickname());
                intent.putExtra("purecontent",dataBean.getPurecontent());
                startActivity(intent);
            }
        });
        startImage.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent=new Intent(getActivity(), PlayVideoActivity.class);
                intent.putExtra("name",name);

                startActivity(intent);
            }
        });
    }

}
